package com.sunday.android.fangdaifu.bukong.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunday.android.fangdaifu.bukong.BaseApp;
import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.adapter.HouseDetailListAdapter;
import com.sunday.android.fangdaifu.bukong.adapter.HouseListAdapter;
import com.sunday.android.fangdaifu.bukong.entity.HouseDetail;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.common.model.ResultDO;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.member.base.BaseFragment;
import com.sunday.member.entity.Base;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


import static com.sunday.common.widgets.EmptyLayout.HIDE_LAYOUT;
import static com.sunday.common.widgets.EmptyLayout.NODATA;


/**
 * Created by 刘涛 on 2017/11/7.
 */

public class HouseDetailFragment extends BaseFragment {


    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @Bind(R.id.emptylayout)
    EmptyLayout emptylayout;

    private int type,status,houseId;
    private long memberId;
        private HouseDetailListAdapter adapter;
    private List<HouseDetail> dataSet=new ArrayList<>();


    public static HouseDetailFragment newInstance(int type,int houseId) {
        HouseDetailFragment fragment = new HouseDetailFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putInt("houseId", houseId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecycler();
    }

    private LinearLayoutManager layoutManager;
    private void initRecycler(){
        refreshLayout.setColorSchemeResources(R.color.material_red, R.color.material_green,
                R.color.material_blue, R.color.material_yellow);
        layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new HouseDetailListAdapter(mContext, dataSet);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).color(Color.parseColor("#999999")).build());
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            houseId = getArguments().getInt("houseId");
        }
    }

    public void updateView(int status){
        this.status=status;
        getData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recycler_list, container, false);
        ButterKnife.bind(this, view);
        return view;
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getData();
    }

    private String plantNo;
    private void getData(){
        memberId= BaseApp.getInstance().getMemberId();
        Call<ResultDO<List<HouseDetail>>> call= ApiClient.getApiAdapter().getHouseDetail(memberId,plantNo,houseId,
                type==0?null:type,status);
        call.enqueue(new Callback<ResultDO<List<HouseDetail>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<HouseDetail>>> call, Response<ResultDO<List<HouseDetail>>> response) {
                if (isFinish||response.body()==null){
                    return;
                }
                refreshLayout.setRefreshing(false);
                if (response.body().getCode()==0||response.body().getResult()==null){
                    dataSet.clear();
                    dataSet.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                    emptylayout.setErrorType(dataSet.isEmpty()?NODATA:HIDE_LAYOUT);
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<HouseDetail>>> call, Throwable t) {
                refreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
